//bsp(即board support packet )
#include	"board.h"
#include  	<rtthread.h>   //这个和rt-thread的内核相关

/**
*	@brief	LED灯IO口初始化函数
*	@param	无
*	@retval 无
*/
void LED_GPIO_Config(void)
{
    /*定义一个GPIO_InitTypeDef类型的结构体*/
    GPIO_InitTypeDef	GPIO_InitStructure;  //(GPIO初始化类型定义)

    /*开启LED相关的GPIO的外设时钟*/
    RCC_APB2PeriphClockCmd(SX1278_RX_LED_GPIO_CLK|SX1278_TX_LED_GPIO_CLK|FAN1_GPIO_CLK|FAN2_GPIO_CLK|FAN3_GPIO_CLK,ENABLE );
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE );
    GPIO_PinRemapConfig( GPIO_Remap_SWJ_JTAGDisable, ENABLE );
    /*开始配置相关的GPIO端口*/
    //因为配置的是同一种模式，故用1个初始化结构体即可  (这里配置的是LED彩灯)
    GPIO_InitStructure.GPIO_Pin=SX1278_RX_LED_GPIO_PIN|SX1278_TX_LED_GPIO_PIN|FAN1_GPIO_PIN|FAN2_GPIO_PIN|FAN3_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    /*调用库函数初始化GPIO*/
    //因要配置的GPIO引脚都在同一个端口故，只用SX1278_TX_LED_GPIO_PORT
    GPIO_Init(SX1278_TX_LED_GPIO_PORT,&GPIO_InitStructure);
	//上电时LED灯全灭
    SX1278_RX_LED_OFF;
    SX1278_TX_LED_OFF;
    FAN1_OFF;
    FAN2_OFF;
    FAN3_OFF;
}


/**
*	@brief	LED接收动作函数
*	@param	无
*	@retval 无
*/
static void  LED_RX_Action(void)
{
    SX1278_RX_LED_ON;
    rt_thread_delay(50);
    SX1278_RX_LED_OFF;
    rt_thread_delay(50);

    SX1278_RX_LED_ON;
    rt_thread_delay(50);
    SX1278_RX_LED_OFF;
    rt_thread_delay(50);

    SX1278_RX_LED_ON;
    rt_thread_delay(50);
    SX1278_RX_LED_OFF;
    rt_thread_delay(50);

    SX1278_RX_LED_ON;
    rt_thread_delay(50);
    SX1278_RX_LED_OFF;
    rt_thread_delay(50);

    SX1278_RX_LED_ON;
    rt_thread_delay(50);
    SX1278_RX_LED_OFF;
    rt_thread_delay(50);
}

/**
*	@brief	LED发送动作函数
*	@param	无
*	@retval 无
*/
static void  LED_TX_Action(void)
{
    SX1278_TX_LED_ON;
    rt_thread_delay(50);
    SX1278_TX_LED_OFF;
    rt_thread_delay(50);

    SX1278_TX_LED_ON;
    rt_thread_delay(50);
    SX1278_TX_LED_OFF;
    rt_thread_delay(50);

    SX1278_TX_LED_ON;
    rt_thread_delay(50);
    SX1278_TX_LED_OFF;
    rt_thread_delay(50);

    SX1278_TX_LED_ON;
    rt_thread_delay(50);
    SX1278_TX_LED_OFF;
    rt_thread_delay(50);

    SX1278_TX_LED_ON;
    rt_thread_delay(50);
    SX1278_TX_LED_OFF;
    rt_thread_delay(50);   
}

/**
*	@brief	LED动作函数，分别完成发送动作，接收动作，开机测试
*	@param	无
*	@retval 无
*/
void  LED_Action(void)
{
    d_struct_mail *led_mail_t;     //LED显示专用的邮箱
    rt_err_t result = RT_EOK;

    result = rt_mb_recv( led_mail,                //邮箱的句柄
                         (rt_uint32_t*)&led_mail_t,    //转换成rt_uint32_t型的指针(这种写法很怪呀)
                         RT_WAITING_FOREVER);        //一直等直到收到消息
    if(result==RT_EOK)
    {
        if((*led_mail_t).index==1)
        {
            LED_TX_Action();
        }
        else if((*led_mail_t).index==2)
        {
            LED_RX_Action();
        }
    }
}




